#include <commands\client\c_send_login.h>
#include <commands\server\s_request_login.h>
#include <app\states\connectionState.h>

REGISTER_COMMAND(SERVER_REQUEST_LOGIN, s_request_login)

s_request_login::s_request_login() : command(0, NULL) {
}

s_request_login::s_request_login(unsigned int _type_id, streamingConnection *_connection) : command(_type_id, _connection) {
}

void 
s_request_login::decode(stringstream &bytes_stream, int size) {
#ifdef CLIENT

  #ifdef DEBUG_INFO
  std::cerr << "CLIENT DECODE: s_request_login" << std::endl;
  #endif

  streamingConnection *conn = getconnection();
  connectionState * cstate = GET_STATE(connectionState, conn);

  c_send_login *cmd = (c_send_login *) commandFactory::instance().createCommand(CLIENT_SEND_LOGIN, conn);
   cmd->setData(cstate->getModelName().c_str(), cstate->getUserName().c_str(), cstate->getPassword().c_str());
   cmd->execute();
  delete cmd;

#endif
}

void 
s_request_login::execute() {
#ifdef SERVER

  #ifdef DEBUG_INFO
  std::cerr << "SERVER EXECUTE: s_request_login" << std::endl;
  #endif

  unsigned int *id = new unsigned int[1];
  unsigned int *total_size = new unsigned int[1];

  total_size[0] = sizeof(unsigned int); // total size itself

  id[0] = getTypeId();
  total_size[0] += sizeof(unsigned int); // id size

  sendBytes(total_size, sizeof(unsigned int));
  sendBytes(id, sizeof(unsigned int));

#endif
}

